package cn.fjsay.java.demo1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JdbcDemo extends Person{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);

        System.out.println("请输入姓名：");
        String pname=sc.next();
        System.out.println("请输入密码：");
        String pwd=sc.next();
        System.out.println("请输入性别：");
        String gender=sc.next();
        System.out.println("请输入地址：");
        String address=sc.next();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/persondb","root","root");
            System.out.println(conn==null?"连接失败":"连接成功");

            String sql = "insert into t_person (pname, pwd, gender, address) values (?, ?, ?, ?)";

            PreparedStatement psmt =conn.prepareStatement(sql);
            psmt.setString(1,pname);
            psmt.setString(2,pwd);
            psmt.setString(3,gender);
            psmt.setString(4,address);
            int count=psmt.executeUpdate();
            System.out.println(count>0?"添加成功":"添加失败");
            psmt.close();
            conn.close();



        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}
